/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.knife.blog.web.action;

import com.knife.util.CommUtil;
import com.knife.util.HtmlUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;
import com.knife.blog.domain.BlogMessage;
import com.knife.blog.domain.BlogNewsDoc;
import com.knife.blog.domain.BlogReview;
import com.knife.blog.domain.Photo;
import com.knife.blog.logic.BlogMessageService;
import com.knife.blog.logic.BlogNewsDocService;
import com.knife.blog.logic.BlogReviewService;
import com.knife.blog.logic.CurrentUser;
import com.knife.blog.logic.impl.BlogCoreServiceImpl;
import com.knife.blog.logic.impl.BlogMessageServiceImpl;
import com.knife.blog.logic.impl.BlogReviewServiceImpl;
import com.knife.blog.logic.impl.PhotoServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class BlogInfoAction extends BaseCmdAction {
    private static final Logger logger = (Logger) Logger.getLogger(BlogMessageAction.class.getName());
    private BlogReviewService blogReviewService = BlogReviewServiceImpl.getInstance();
    private BlogMessageService blogMessageService = BlogMessageServiceImpl.getInstance();
    private BlogNewsDocService blogDocService = BlogCoreServiceImpl.getInstance();

    public Page doInit(WebForm form, Module module) {
        return null;
    }

    @Override
    public Object doBefore(WebForm form, Module module) {
        IActiveUser user = CurrentUser.getActiveUser();
        if (user != null) {
            blog = userService.getUserBlog(user.getUserName());
        }
        return super.doBefore(form, module);
    }

    public Page doBlogReadTimes(WebForm form, Module module){
        int ret=0;
        String username = CommUtil.null2String(form.get("user"));
        try{
            blog = userService.getUserBlog(username);
            ret=blog.getReadtimes();
        }catch(Exception e){
        }
        form.addResult("result", ret);
        return module.findPage("ajaxResult");
    }

    public Page doCountUnCheckReview(WebForm form, Module module){
        int ret=0;
        String username = CommUtil.null2String(form.get("user"));
        try{
            if(blog.getUsername().equals(username)){
                ret = blogReviewService.countUnCheckMessage(blog.getUsername());
            }
        }catch(Exception e){}
        form.addResult("result", ret);
        return module.findPage("ajaxResultReview");
    }

    public Page doCountUnCheckMessage(WebForm form, Module module){
        int ret=0;
        String username = CommUtil.null2String(form.get("user"));
        try{
            if(blog.getUsername().equals(username)){
                ret = blogMessageService.countUnCheckMessage(blog.getCid());
            }
        }catch(Exception e){}
        form.addResult("result", ret);
        return module.findPage("ajaxResultMessage");
    }

    public Page doTopMessage(WebForm form, Module module){
        String username = CommUtil.null2String(form.get("user"));
        int number = CommUtil.null2Int(form.get("num"));
        if(number<=0){
            number=1;
        }
        List list = new ArrayList();
        try{
            list=blogMessageService.getTopMessage(username,number);
        }catch(Exception e){
            //e.printStackTrace();
        }
        form.addResult("user", username);
        form.addResult("list", list);
        return module.findPage("ajaxResultListMessage");
    }

    public Page doTopReview(WebForm form, Module module){
        String username = CommUtil.null2String(form.get("user"));
        int number = CommUtil.null2Int(form.get("num"));
        if(number<=0){
            number=1;
        }
        List list = new ArrayList();
        try{
            list=blogReviewService.getTopReviews(username,number);
        }catch(Exception e){
            //e.printStackTrace();
        }
        //form.addResult("user", username);
        form.addResult("list", list);
        return module.findPage("ajaxResultListReview");
    }

    public Page doTopDocReview(WebForm form, Module module){
        String pid = CommUtil.null2String(form.get("pid"));
        int number = CommUtil.null2Int(form.get("num"));
        if(number<=0){
            number=1;
        }
        List list = new ArrayList();
        try{
            list=blogReviewService.queryBlogReview(pid, 1, number).getResult();
        }catch(Exception e){
            logger.error(e);
            //e.printStackTrace();
        }
        form.addResult("list", list);
        return module.findPage("ajaxResultListReview");
    }

    public Page doShowReview(WebForm form, Module module) {
        String pid = CommUtil.null2String(form.get("pid"));
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 15;
        }
        IPageList pageList = this.blogReviewService.queryBlogReview(pid, currentPage, pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        if (pageList != null) {
            form.addResult("pageSize", new Integer(pageSize));
        }
        String type = CommUtil.null2String(form.get("type"));
        if (type.equals("photo")) {
            Photo obj = PhotoServiceImpl.getInstance().getPhoto(pid);
            form.addResult("obj", obj);
            this.blog = this.userService.getUserBlog(obj.getInputuser());
            return new Page("review" + this.blog.getUsername(), "/user/photoreviewShow.htm", "template");
        }
        BlogNewsDoc obj = this.blogDocService.getBlogNewsDoc(pid);
        form.addResult("obj", obj);
        this.blog = this.userService.getUserBlog(obj.getInputuser());
        return new Page("review" + this.blog.getUsername(), "/user/reviewShow.htm", "template");
    }

    public Page doPostReview(WebForm form, Module module) {
        BlogReview obj = (BlogReview) form2Obj(form);
        String type = (String) form.get("type");
        boolean ret = false;
        if (type.equals("doc")) {
            ret = this.blogReviewService.addBlogReview(obj);
        }
        if (type.equals("photo")) {
            ret = this.blogReviewService.addPhotoReview(obj);
        }
        form.addResult("msg", (ret) ? "评论成功,审核中!" : "评论失败，可能是权限不够或者输入的内容不合法!");
        String url = "/blogInfo.do?parameter=showReview&type=" + type + "&pid=" + obj.getParentid();
        form.addResult("url", url);
        form.addResult("time", "1");
        return new Page("postOk", url, "url");
    }
    
    public Page doPostMessage(WebForm form, Module module) {
        boolean ret=false;
        String belonguser = (String) form.get("belonguser");
        if ((belonguser == null) || (belonguser.equals(""))) {
            this.blog = this.userService.getUserBlog(getCurrentUser(form).getUserName());
        } else {
            this.blog = this.userService.getUserBlog(belonguser);
        }
        try{
            BlogMessage obj = (BlogMessage) form2ObjMessage(form);
            obj.setBelonguser((String)form.get("belongCid"));
            obj.setAuditing(Integer.valueOf(0));
            ret = this.blogMessageService.add(obj);
        }catch(Exception e){
            ret = false;
            logger.error(e);
        }
        form.addResult("msg", (ret) ? "留言成功，审核中" : "留言失败");
        //String url = "/" + this.blog.getDomain() + "/message.htm";
        form.addResult("url", "/" + this.blog.getDomain() + "/message.htm");
        form.addResult("time", "1");
        String url = "/user/message.htm";
        return new Page("postOk", url, "url");
    }

    public Object form2ObjMessage(WebForm form) {
        String cid = CommUtil.null2String(form.get("cid"));
        BlogMessage obj = this.blogMessageService.getBlogMessage(cid);
        if (obj == null) {
            obj = new BlogMessage();
        }
        form.toPo(obj);
        obj.setBelonguser(HtmlUtil.escapeHTMLTag(obj.getBelonguser()));
        obj.setCid(HtmlUtil.escapeHTMLTag(obj.getCid()));
        obj.setContent(HtmlUtil.escapeHTMLTag(obj.getContent()));
        obj.setInputuser(HtmlUtil.escapeHTMLTag(obj.getInputuser()));
        return obj;
    }

    public Object form2Obj(WebForm form) {
        String cid = CommUtil.null2String(form.get("cid"));
        BlogReview obj = this.blogReviewService.getBlogReview(cid);
        if (obj == null) {
            obj = new BlogReview();
        }
        form.toPo(obj);
        return obj;
    }
}